/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.snowstack.collectioncase;

import cn.icanci.snowstack.Action;
import cn.icanci.snowstack.CommonMachineExecute;
import cn.icanci.snowstack.collectioncase.exception.CollectionInvalidActionException;
import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author icanci
 * @date Created in 2021/05/23 23:28
 * @version 1.0
 */
@Data
public class CollectionEntranceMachine implements CommonMachineExecute {
    List<EntranceMachineTransaction>       entranceMachineTransactionList = Arrays.asList(
        EntranceMachineTransaction.builder().currentState(CollectionEntranceMachineState.LOCKED).action(CollectionAction.INSERT_COIN)
            .nextState(CollectionEntranceMachineState.UNLOCKED).event(new OpenEvent()).build(),
        EntranceMachineTransaction.builder().currentState(CollectionEntranceMachineState.LOCKED).action(CollectionAction.PASS).nextState(CollectionEntranceMachineState.LOCKED)
            .event(new AlarmEvent()).build(),
        EntranceMachineTransaction.builder().currentState(CollectionEntranceMachineState.UNLOCKED).action(CollectionAction.PASS).nextState(CollectionEntranceMachineState.LOCKED)
            .event(new ClosedEvent()).build(),
        EntranceMachineTransaction.builder().currentState(CollectionEntranceMachineState.UNLOCKED).action(CollectionAction.INSERT_COIN)
            .nextState(CollectionEntranceMachineState.UNLOCKED).event(new RefundEvent()).build());
    private CollectionEntranceMachineState state;

    public CollectionEntranceMachine(CollectionEntranceMachineState state) {
        setState(state);
    }

    /**
     * 执行
     * @param action
     * @return 返回结果状态
     */
    @Override
    public String execute(Action action) {
        CollectionAction collectionAction = (CollectionAction) action;
        Optional<EntranceMachineTransaction> transactionOptional = entranceMachineTransactionList.stream()
            .filter(transaction -> transaction.getAction().equals(collectionAction) && transaction.getCurrentState().equals(state)).findFirst();

        if (!transactionOptional.isPresent()) {
            throw new CollectionInvalidActionException();
        }

        EntranceMachineTransaction transaction = transactionOptional.get();
        setState(transaction.getNextState());
        return transaction.getEvent().execute();
    }
}
